import { HOST,URL } from '/utils/api.js'
import { showError} from '/utils/util.js'
App({
  onLaunch: function () {},
  globalData: {
    userInfo: null,
    hasUserInfo: false,
    qrCodeInfo: 'dzy-k8qQiyluNC7tUm90Fx76sFPixYuJrV8cz7UPfSGw', //扫码二维码信息
    appId:'' ,   // 小程序appid
    terminalCode:'', // 机器名
    orderCode:'', // 订单号
    message:'',// 向蓝牙发送的数据
  },
  // 用户授权登录
  login: function (fn, noLogin) {
    let that = this;
    wx.getUserInfo({
      success: (res) => {
        let user = (that.globalData.userInfo = res.userInfo);
        // 从project.config.json配置文件中获取openid
        let accountInfo = wx.getAccountInfoSync();
        that.globalData.appId = accountInfo.miniProgram.appId;
        if (noLogin && typeof noLogin == 'function') {
            noLogin();
        }
        wx.login({
          success: result => {
            let data = {
              jscode: result.code,
              appId: that.globalData.appId,
              qrCode:that.globalData.qrCodeInfo
            }
            that._get(URL.login, data, rest => {        
              if (rest.data.code !== '00'){
                  wx.showToast({
                    title: res.data.message,
                    icon: 'none'
                  })
                  return false;
                }
                /**
                 * 判断手机系统
                 */
                wx.getSystemInfo({
                  success: function(res) {
                    console.log(res);
                    if (res.platform === "ios"){    // 苹果
                      let index = rest.header['Set-Cookie'].indexOf(";");
                      let cookieVal = rest.header['Set-Cookie'].substring(8, index);
                      wx.setStorageSync('cookie', cookieVal);
                    }
                    else if (res.platform === "android"){ // 安卓
                      let index = rest.header['set-cookie'].indexOf(";");
                      let cookieVal = rest.header['set-cookie'].substring(8, index);
                      wx.setStorageSync('cookie', cookieVal);
                    }
                    else{                         // 开发者工具PC
                      let index = rest.header['set-cookie'].indexOf(";");
                      let cookieVal = rest.header['set-cookie'].substring(8, index);
                      wx.setStorageSync('cookie', cookieVal);
                    }
                  },
                })
               
                fn && typeof fn == 'function' && fn();
                if (noLogin && typeof noLogin == 'function') {
                    wx.hideLoading()
                }
            })
          },
        })
      }
    })
  },

  /**
   * get请求
   */
  _get: function (url, data, success, fail, complete) {
    this.httpRequest('GET', url, data, success, fail, complete)
  },
  /**
   * post请求
   */
  _post: function (url, data, success, fail, complete) {
    this.httpRequest('POST', url, data, success, fail, complete)
  },
  /**
   * 请求封装
   */
  httpRequest: function (type, url, data, success, fail, complete) {
    data = data || {};
    let App = this;
    wx.request({
      url: HOST + url,
      header: {
        'Cookie': wx.getStorageSync("cookie") ? `SESSION=${wx.getStorageSync("cookie")}` : '',
      },
      method: type,
      data:data,
      success: function (res) {
        wx.hideLoading()
        if (res.data.code === 'QR0003'){
            App.login(function () {
                if(type === 'GET'){
                  App._get(url, data, success, fail, complete)
                }
                else{
                  App._post(url, data, success, fail, complete)
                }
              }, true);
        }
        else if(res.data.code !== '00'){
            showError('友情提醒',res.data.message);
            
        }
        else{
            success && success(res);
        }
      },
      fail,
      complete
    })
  },
})